package com.superatom;

import java.util.Random;

public class SingleClassInstance {

    public static void main(String[] args){

        int a = 8;
        int b = a;
        System.out.println("a=" + a);
        System.out.println("b=" + b);

        // 对于基本数据类型，a和b变量存储的是实际值，而不是引用
        b = 2;
        System.out.println("a=" + a);
        System.out.println("b=" + b);


        User user1 = new User();
        User user2 = new User();

        user1.name = "tom";
        user2.name = "mary";

        // 各自创建一个User对象，打印各自的名称
        System.out.println("User1's name " + user1.name);
        System.out.println("User2's name " + user2.name);

        // 把user2赋值给user1，其实就是引用的复制，地址的复制，
        // 所以user1和user2保存相同的引用。
        // 原本user1指向的对象实例就会被垃圾回收器自动清理
        user1 = user2;
        System.out.println("User1's name " + user1.name);
        System.out.println("User2's name " + user2.name);

        // 表明user1与user2其实是指向了同一个对象实例
        user1.name = "Lucy";
        System.out.println("User1's name " + user1.name);
        System.out.println("User2's name " + user2.name);

        // 对于基本类型的对象其实也保存基本类型的特征，变量保留的是值而不是引用
        Integer ia = 3;
        Integer ib = ia;
        System.out.println("ia=" + ia);
        System.out.println("ib=" + ib);

        ib = 4;
        System.out.println("ia=" + ia);
        System.out.println("ib=" + ib);

        Random random = new Random();
        System.out.println(random.nextInt(5));

    }
}

class User{
    String name;
}
